package src.config;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @author CsCN
 * 加载参数
 */
public class SysConfig {
    /**
     * 系统固定参数
     */
    private static final String LOCAL_PATH = "./src/config/";
    private static final String SYS_PROFILE = LOCAL_PATH + "system.properties";
    private static final Charset SYS_CHARSET = StandardCharsets.UTF_8;

    /**
     * 系统配置参数
     */
    public static int SERVER_PORT = 80;
    public static int SERVER_TIMEOUT = -1;
    public static int LOGIN_PORT = 80;
    public static int MYSQL_START = 1;
    public static String MYSQL_IP = "127.0.0.1";
    public static int MYSQL_PORT = 3306;
    public static String MYSQL_USERNAME = "root";
    public static String MYSQL_PASSWORD = "root";
    public static String MYSQL_USERS_TABLE_NAME = "users";
    public static String MYSQL_DATABASE = MYSQL_USERNAME;
    public static String MYSQL_SCORE_TABLE_NAME = "score";

    // 初始化配置参数
    static {
        initParams();
    }

    /**
     * 加载配置文件
     */
    private static void initParams() {
        Properties props = new Properties();
        // read放在()里面, 可以自动close
        try (Reader reader = new InputStreamReader(new FileInputStream(SYS_PROFILE), SYS_CHARSET)) {
            props.load(reader);
            SERVER_PORT = Integer.parseInt(props.getProperty("system.serverPort", Integer.toString(SERVER_PORT)));
            SERVER_TIMEOUT = Integer.parseInt(props.getProperty("system.serverTimeout", Integer.toString(SERVER_TIMEOUT)));
            LOGIN_PORT = Integer.parseInt(props.getProperty("system.loginPort", Integer.toString(LOGIN_PORT)));
            MYSQL_START = Integer.parseInt(props.getProperty("mysql.start", Integer.toString(MYSQL_START)));
            MYSQL_PORT = Integer.parseInt(props.getProperty("mysql.port", Integer.toString(MYSQL_PORT)));
            MYSQL_IP = props.getProperty("mysql.ip", MYSQL_IP);
            MYSQL_USERNAME = props.getProperty("mysql.username", MYSQL_USERNAME);
            MYSQL_PASSWORD = props.getProperty("mysql.password", MYSQL_PASSWORD);
            MYSQL_DATABASE = props.getProperty("mysql.database", MYSQL_DATABASE);
            MYSQL_USERS_TABLE_NAME = props.getProperty("mysql.usersTableName", MYSQL_USERS_TABLE_NAME);
            MYSQL_SCORE_TABLE_NAME = props.getProperty("mysql.scoreTableName", MYSQL_SCORE_TABLE_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
